#define _XOPEN_SOURCE 800
#include "apue.h"
#include <pwd.h>
#include <stddef.h>
#include <string.h>

int main(int argc, char *argv[]) {
    struct passwd *ptr;    
    if (argc != 2)
        err_quit("Usage: ./a.out <username>");

    if ((ptr= getpwnam(argv[1])) != NULL)
        printf("uid of %s is %d\n", argv[1], ptr->pw_uid);
    else
        printf("user %s is not exist\n", argv[1]);
}

struct passwd * getpwnam(const char *name)
{
    static struct passwd *ptr;
    setpwent();
    while((ptr= getpwent()) != NULL)
        if (strcmp(ptr->pw_name, name) == 0)
            break;

    endpwent();

    return ptr;
}
